﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;

namespace ZHDJ.AppInterface.Controllers
{
    public class MassesVoiceController : ApiController
    {
        #region 获取自增ID
        [HttpPost]
        public ReturnResult GetID()
        {
            return new ReturnResult()
            {
                code = "200",
                result = StringHelper.NewID,
                msg = "success"
            };
        }
        #endregion

        #region 上传图片
        [HttpPost]
        public ReturnResult UpImage(dynamic obj)
        {
            ReturnResult model = new ReturnResult();
            string image = Convert.ToString(obj.imgurl);//Base64字符编码图片
            string Extension = Convert.ToString(obj.Extension);//扩展名

            ImageModel imageModel = InterfaceHelper.UpImge(image, Extension);
            if (imageModel != null)
            {
                model.code = "200";
                model.result = imageModel;
                model.msg = "上传成功！";
            }
            else
            {
                model.code = "201";
                model.msg = "上传失败！";
            }

            return model;
        }
        #endregion

        #region 上传图片
        [HttpPost]
        public ReturnResult UpImageExtend(dynamic obj)
        {
            ReturnResult model = new ReturnResult();
            string image = Convert.ToString(obj.imgurl);//Base64字符编码图片
            string Extension = Convert.ToString(obj.Extension);//扩展名

            ImageModel imageModel = InterfaceHelper.UpImgeExtend(image, Extension);
            if (imageModel != null)
            {
                model.code = "200";
                model.result = imageModel;
                model.msg = "上传成功！";
            }
            else
            {
                model.code = "201";
                model.msg = "上传失败！";
            }

            return model;
        }
        #endregion

        #region 上传图片(加水印)
        [HttpPost]
        public ReturnResult UpImageWaterMark(dynamic obj)
        {
            ReturnResult model = new ReturnResult();
            string image = Convert.ToString(obj.imgurl);//Base64字符编码图片
            string Extension = Convert.ToString(obj.Extension);//扩展名
            string WaterMarkContent = Convert.ToString(obj.WaterMarkContent);//水印文字

            ImageModel imageModel = InterfaceHelper.UpImageWaterMark(image, Extension, WaterMarkContent);
            if (imageModel != null)
            {
                model.code = "200";
                model.result = imageModel;
                model.msg = "上传成功！";
            }
            else
            {
                model.code = "201";
                model.msg = "上传失败！";
            }

            return model;
        }
        #endregion

        #region 群众心声保存方法
        [HttpPost]
        public ReturnResult Save(dynamic obj)
        {
            ReturnResult model = new ReturnResult();
            try
            {
                var saveModel = new PartyGrassrootsSuggestion();
                saveModel.ID = StringHelper.NewID;
                saveModel.Title = Convert.ToString(obj.cTitle);
                saveModel.MobileNo = Convert.ToString(obj.MobileNo);
                saveModel.UserName = Convert.ToString(obj.UserName);
                saveModel.CollectionTime = DateTime.Now;
                saveModel.Content = Convert.ToString(obj.content);
                saveModel.Status = 1;
                saveModel.CreateTime = DateTime.Now;
                saveModel.CreateBy = Convert.ToString(obj.CreateBy);
                saveModel.ModifyTime = DateTime.Now;
                saveModel.publicity = Convert.ToBoolean(obj.Publicity) ? 1 : 0;
                saveModel.OneLayer = Convert.ToString(obj.OneLayer);
                saveModel.TwoLayer = Convert.ToString(obj.TwoLayer);
                saveModel.ThreeLayer = Convert.ToString(obj.ThreeLayer);
                //saveModel.Image = Convert.ToString(obj.ImageUrl);
                InterfaceHelper.MassesVoiceSave(saveModel);
                //批量添加文件表
                string[] array = Convert.ToString(obj.imgString).TrimEnd(',').Split(',');
                for (int i = 0; i < array.Length; i++)
                {
                    PartyFile file = new PartyFile();
                    file.ID = StringHelper.NewID;
                    file.FID = saveModel.ID;
                    file.Title = saveModel.Title + i.ToString();
                    file.Extension = ".jpg";
                    file.Description = saveModel.Title + i.ToString();
                    file.Size = 0;
                    file.Url = array[i];
                    file.CreateTime = DateTime.Now;
                    file.CreateBy = saveModel.CreateBy;
                    file.ModifyTime = DateTime.Now;
                    file.ModifyBy = saveModel.CreateBy;
                    InterfaceHelper.AddFileTalbe(file);
                }
                model.code = "200";
                model.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                model.code = "201";
                model.msg = "保存失败！";
            }
            return model;
        }
        #endregion

        #region 获取群众心声数据
        [HttpPost]
        public ReturnResult GetMassesVoice(dynamic obj)
        {
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            DataTable dt = InterfaceHelper.GetMassesVoice(pageIndex, pageSize);
            IList<PartyGrassrootsSuggestion> list = new List<PartyGrassrootsSuggestion>();
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyGrassrootsSuggestion>.ConvertToModel(dt);

            foreach (PartyGrassrootsSuggestion model in list)
            {
                DataTable dtfile = InterfaceHelper.GetPartyFileByFID(model.ID);
                List<string> newlist = new List<string>();
                foreach (DataRow item in dtfile.Rows)
                {
                    newlist.Add(item["Url"].ToString());
                }
                model.PicList = newlist;
            }
            return new ReturnResult
            {
                code = "200",
                result = list,
                msg = "success"
            };
        }
        #endregion

        #region 获取群众心声实体
        [HttpPost]
        public ReturnResult GetMassesVoiceModel(dynamic obj)
        {
            string ID = Convert.ToString(obj.ID);
            PartyGrassrootsSuggestion model = InterfaceHelper.GetMassesVoiceModel(ID);
            return new ReturnResult()
            {
                code = "200",
                result = model,
                msg = "success"
            };
        }
        #endregion

        #region 获取群众心声详情数据
        [HttpPost]
        public ReturnResult GetMassesVoiceDetail(dynamic obj)
        {
            string SID = Convert.ToString(obj.SID);
            DataTable dt = InterfaceHelper.GetMassesVoiceDetail(SID);
            IList<PartyGrassrootsSuggestionDetail> list = new List<PartyGrassrootsSuggestionDetail>();
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyGrassrootsSuggestionDetail>.ConvertToModel(dt);
            return new ReturnResult
            {
                code = "200",
                result = list,
                msg = "success"
            };
        }
        #endregion

        #region 爱心认领
        [HttpPost]
        public ReturnResult LoveClaim(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            PartyGrassrootsSuggestion model = new PartyGrassrootsSuggestion();
            string userName = Convert.ToString(obj.UserName);
            model.ID = Convert.ToString(obj.ID);
            model.Status = 2;
            model.Unclaimed = Convert.ToString(obj.CreateBy);
            PartyGrassrootsSuggestionDetail dmodel = new PartyGrassrootsSuggestionDetail();
            dmodel.ID = StringHelper.NewID;
            dmodel.SID = model.ID;
            dmodel.Content = "心声被" + userName + "领取";
            dmodel.CreateBy = Convert.ToString(obj.CreateBy);
            dmodel.CreateTime = DateTime.Now;
            dmodel.Status = 2;
            if (InterfaceHelper.UpdateMassesVoiceStatusByID(model) > 0)
            {
                //添加心声详情表
                InterfaceHelper.AddMassesVoiceDetail(dmodel);
                result.code = "200";
                result.msg = "爱心认领成功";
            }
            else
            {
                result.code = "201";
                result.msg = "爱心认领失败";
            }
            return result;
        }
        #endregion

        #region 获取认领人群众心声数据
        [HttpPost]
        public ReturnResult GetUnclaimedMassesVoice(dynamic obj)
        {
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string CreateBy = Convert.ToString(obj.CreateBy);
            DataTable dt = InterfaceHelper.GetUnclaimedMassesVoice(pageIndex, pageSize, CreateBy);
            IList<PartyGrassrootsSuggestion> list = new List<PartyGrassrootsSuggestion>();
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyGrassrootsSuggestion>.ConvertToModel(dt);

            foreach (PartyGrassrootsSuggestion model in list)
            {
                DataTable dtfile = InterfaceHelper.GetPartyFileByFID(model.ID);
                List<string> newlist = new List<string>();
                foreach (DataRow item in dtfile.Rows)
                {
                    newlist.Add(item["Url"].ToString());
                }
                model.PicList = newlist;
            }
            return new ReturnResult
            {
                code = "200",
                result = list,
                msg = "success"
            };
        }
        #endregion

        #region 提交审核
        [HttpPost]
        public ReturnResult SaveApply(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            PartyGrassrootsSuggestion model = new PartyGrassrootsSuggestion();
            PartyGrassrootsSuggestionDetail dmodel = new PartyGrassrootsSuggestionDetail();
            int status = Convert.ToInt32(obj.Status);
            try
            {
                #region 业务逻辑
                if (status == 1)//未处理/放弃
                {
                    //更改主表状态并删除详情表数据
                    model.ID = Convert.ToString(obj.SID);
                    model.Status = status;
                    model.Unclaimed = "";
                    InterfaceHelper.UpdateMassesVoiceStatusByID(model);
                    InterfaceHelper.DeleteMassesVoiceDetailBySID(model.ID);
                    result.code = "200";
                    result.msg = "放弃成功";
                }
                else if (status == 2)//添加进度/进行中
                {
                    //只添加详情表
                    dmodel.ID = StringHelper.NewID;
                    dmodel.SID = Convert.ToString(obj.SID);
                    dmodel.Content = Convert.ToString(obj.Content);
                    dmodel.CreateBy = Convert.ToString(obj.CreateBy);
                    dmodel.CreateTime = DateTime.Now;
                    dmodel.Status = status;
                    InterfaceHelper.AddMassesVoiceDetail(dmodel);
                    result.code = "200";
                    result.msg = "添加进度成功";
                }
                else if (status == 3)//处理完成
                {
                    //更改主表状态并添加详情表
                    model.ID = Convert.ToString(obj.SID);
                    model.Status = status;
                    model.Unclaimed = Convert.ToString(obj.CreateBy);

                    dmodel.ID = StringHelper.NewID;
                    dmodel.SID = Convert.ToString(obj.SID);
                    dmodel.Content = Convert.ToString(obj.Content);
                    dmodel.CreateBy = Convert.ToString(obj.CreateBy);
                    dmodel.CreateTime = DateTime.Now;
                    dmodel.Status = status;
                    InterfaceHelper.UpdateMassesVoiceStatusByID(model);
                    InterfaceHelper.AddMassesVoiceDetail(dmodel);
                    result.code = "200";
                    result.msg = "处理完成";
                }
                #endregion
            }
            catch (Exception ex)
            {
                result.code = "201";
                result.msg = "操作失败";
            }
            return result;
        }
        #endregion
    }
}
